package com.itfreer.file.upload.service;

import java.io.File;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 定义上传日志服务类
 */
public class UploadLogService {
	private DBOpenHelper dbOpenHelper;
	
	public UploadLogService(Context context){
		this.dbOpenHelper = new DBOpenHelper(context);
	}

	/**
	 * 保存状态
	 * @param sourceid
	 * @param uploadFile
	 */
	public void save(String sourceid, File uploadFile){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execSQL("insert into uploadlog(uploadfilepath, sourceid) values(?,?)",
				new Object[]{uploadFile.getAbsolutePath(),sourceid});
	}

	/**
	 * 删除记录
	 * @param uploadFile
	 */
	public void delete(File uploadFile){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execSQL("delete from uploadlog where uploadfilepath=?", new Object[]{uploadFile.getAbsolutePath()});
	}

	/**
	 * 获取资源ID
	 * @param uploadFile
	 * @return
	 */
	public String getBindId(File uploadFile){
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select sourceid from uploadlog where uploadfilepath=?", 
				new String[]{uploadFile.getAbsolutePath()});
		if(cursor.moveToFirst()){
			return cursor.getString(0);
		}
		return null;
	}
}
